﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;

namespace klipboardtranslator
{
    class Translator
    {
       private string strTranslationLinkStart;
        private string strTranslationLinkEnd;
        private string strFirstDelimiter;
        private string strLastDelimeter;
        private string strLanguagePair;

        public Translator(string strLanguagePair)
        {
            strTranslationLinkStart = "http://www.google.com/translate_t?hl=en&ie=UTF8&text=";
            strTranslationLinkEnd = "&langpair=";
            //strLanguagePair = "nl|en";
            strFirstDelimiter = "<input type=hidden name=gtrans value=";
            strLastDelimeter = "><div id=sug_exp>";
            this.strLanguagePair = strLanguagePair;
        }

        /// <summary>
        /// Translate Text using Google Translate API's
        /// Google URL - http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}
        /// </summary>
        /// <param name="input">Input string</param>
        /// <returns>Translated to String</returns>

        public string TranslateText(string strInput)
        {
            int iIndexStart = 0, iIndexEnd =0;
            //string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
            string strUrl = strTranslationLinkStart + strInput + strTranslationLinkEnd + strLanguagePair;
            WebClient webClient = new WebClient();
            webClient.Encoding = System.Text.Encoding.UTF8;
            string result = webClient.DownloadString(strUrl);

            bool blValidResult = result.Contains(strFirstDelimiter);
            blValidResult = blValidResult & result.Contains(strLastDelimeter);

            if (blValidResult)
            {
                iIndexStart = result.IndexOf(strFirstDelimiter) + strFirstDelimiter.Length;
                iIndexEnd = result.IndexOf(strLastDelimeter);
                result = result.Substring(iIndexStart, iIndexEnd - iIndexStart);
            }
            else
            {
                throw (new Exception("Unexpected response from the google translate website. Please report the issue at http://code.google.com/p/klipboardtranslator/issues/list"));
            }

            //cleanup result
            result = HttpUtility.HtmlDecode(result);   

            //more cleanup
            result = CustomCleanup(result);


            return result;
        }

        private string CustomCleanup(string input)
        {
            //get rid of stuff between <,> like <br>
            bool blRecord = true;
            string result= "";
            char c;

            int j = 0;

            for (int i = 0; i<input.Length; i++)
            {
                c = input[i];
                if (c == '<')
                {
                    blRecord = false;
                }

                if (blRecord)
                {
                    result += input[i];
                    j++;
                }

                if (c == '>')
                {
                    blRecord = true;
                }

            }

            return result;
        }

    }
}
